package com.em.sugarfight.entity;

import com.em.sugarfight.sugarEnums.Direction;
import lombok.Data;

@Data
public class Position {
    int x;
    int y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    /**
     * 朝指定方向移动
     */
    public Position move(Direction direction) {
        int[] delta = direction.getDelta();
        return new Position(x + delta[0], y + delta[1]);
    }

    /**
     * 计算到另一个位置的曼哈顿距离
     */
    public int manhattanDistance(Position other) {
        return Math.abs(x - other.x) + Math.abs(y - other.y);
    }

    @Override
    public boolean equals(Object o){
        // 如果是同一个对象，直接返回true
        if (this == o) return true;
        // 如果对象为null或类型不同，返回false
        if (o == null || getClass() != o.getClass()) return false;
        // 强制类型转换后比较属性值
        Position position = (Position) o;
        return x == position.x && y == position.y;
    }

    @Override
    public int hashCode() {
        return this.x*100 + this.y;
    }
}
